package com.temple.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
@Component
public class UserClientService {
    @Autowired
    private RestTemplate restTemplate;

    private final String userServiceUrl = "http://127.0.0.1:9090/User/";
    private final String minioServiceUrl = "http://127.0.0.1:9000/minio/upload";

    private final String token = "eyJhbGciOiJIUzUxMiJ9.eyJpZGVudGl0eSI6LTEsInVzZXJQaG9uZSI6IjExMCIsInVzZXJOYW1lIjoiVFEwMSIsIlVVSUQiOiI3N2M4ZGY0My1mOGRhLTRjOGItYmJiYy03MTY2ZWQ3M2NhMGIiLCJ1c2VySWQiOjE4MzMxMjkyMjg2MDY1Mzc3MzB9.WzSaKr4xWwVxVgM7SIRmoiHox7-1uVDmHtwOcX3b2f6ZIv4HEkTC-923tyzuO_WUlkOsAY07CGyvOhuYklwFfg";
    /**
     * @description:获取登录的用户的姓名
     * @param id
     * @return: String
    **/
    public String getUserById(Long id) {
        // 创建 HttpHeaders 对象并设置 Authorization 头
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(token);  // 使用 Bearer Token

        // 创建 HttpEntity 对象
        HttpEntity<String> entity = new HttpEntity<>(headers);

        // 发起带有 token 的请求
        ResponseEntity<String> response = restTemplate.exchange(
                userServiceUrl + "/getName/" + id,
                HttpMethod.GET,
                entity,
                String.class
        );
        return response.getBody();
    }
    public String getPhotoById(Long id) {
        // 创建 HttpHeaders 对象并设置 Authorization 头
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(token);  // 使用 Bearer Token

        // 创建 HttpEntity 对象
        HttpEntity<String> entity = new HttpEntity<>(headers);

        // 发起带有 token 的请求
        ResponseEntity<String> response = restTemplate.exchange(
                userServiceUrl + "/getPhotoInfo/" + id,
                HttpMethod.GET,
                entity,
                String.class
        );
        return response.getBody();
    }


    /**
     * @description:将图片上传给minio服务存储器
     * @param file
     * @return: String
    **/
    public String setFile(MultipartFile file){
        // 创建 HttpHeaders 对象并设置 Authorization 头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        // 创建 MultiValueMap 并添加文件
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", file.getResource());

        // 创建 HttpEntity 对象，包括文件内容和头信息
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, headers);

        // 发起带有 token 和文件内容的请求
        ResponseEntity<String> response = restTemplate.exchange(
                minioServiceUrl,
                HttpMethod.POST,  // 假设你是用 POST 方法上传文件
                entity,
                String.class
        );

        return response.getBody();
    }




}
